from rest_framework import serializers

from prj003.models import (
    questionnaire, summary, history,
    relevant, clinical, cure, results
)
from utils.samecode import ExtendsSerializer, SCSerializer
from utils.savemobile import create_object


class QuestionNaireOutSerializer(ExtendsSerializer, serializers.ModelSerializer):

    class Meta:
        model = questionnaire.QuestionNaire
        exclude = ['id', 'create_time']
        read_only_fields = ['serial', 'check_status', 'reason_for_check']
        project_name = 'prj003'
        save_mobile = True


class ResultsOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = results.Results
        exclude = ['id', 'owner', 'info', 'create_time']


class CureOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = cure.Cure
        project_name = 'prj003'
        exclude = ['id', 'owner', 'info', 'create_time']


class ClinicalOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = clinical.Clinical
        exclude = ['id', 'owner', 'info', 'create_time']


class RelevantOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = relevant.Relevant
        exclude = ['id', 'owner', 'info', 'create_time']


class HistoryOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = history.History
        exclude = ['id', 'owner', 'info', 'create_time']


class SummaryOutSerializer(serializers.ModelSerializer):

    class Meta:
        model = summary.Summary
        exclude = ['id', 'owner', 'info', 'create_time']


class AllOutSerializer(serializers.Serializer):
    info = QuestionNaireOutSerializer()
    summary = SummaryOutSerializer()
    history = HistoryOutSerializer()
    relevant = RelevantOutSerializer()
    clinical = ClinicalOutSerializer()
    cure = CureOutSerializer()
    results = ResultsOutSerializer()


class MobileSerializer(serializers.Serializer):
    info = QuestionNaireOutSerializer(required=True)
    summary = SummaryOutSerializer(required=False)
    history = HistoryOutSerializer(required=False)
    relevant = RelevantOutSerializer(required=False)
    clinical = ClinicalOutSerializer(required=False)
    cure = CureOutSerializer(required=False)
    results = ResultsOutSerializer(required=False)

    def create(self, instance):
        info = instance.get('info')
        info_owner = info.pop('owner')
        info['owner'] = info_owner.id

        info_serializer = QuestionNaireOutSerializer(data=info)
        info_serializer.is_valid(raise_exception=True)
        info_instance = info_serializer.save()

        summary = instance.get('summary')
        history = instance.get('history')
        relevant = instance.get('relevant')
        clinical = instance.get('clinical')
        cure = instance.get('cure')
        results = instance.get('results')

        create_object(info_instance, summary, SummaryOutSerializer)

        create_object(info_instance, history, HistoryOutSerializer)
        create_object(info_instance, relevant, RelevantOutSerializer)
        create_object(info_instance, results, ResultsOutSerializer)
        create_object(info_instance, cure, CureOutSerializer)
        create_object(info_instance, clinical, ClinicalOutSerializer)

        return instance


class SearchSerializer(serializers.ModelSerializer):
    """ For search api """
    patient_birth = serializers.CharField(required=False, help_text='出生年份')
    career = serializers.CharField(required=False, help_text='职业')
    patient_name = serializers.CharField(required=False, help_text='姓名')
    patient_phone = serializers.CharField(required=False, help_text='手机号')
    hospital_name = serializers.CharField(required=False, help_text='所在医院')
    # address = serializers.CharField(required=False, help_text='地址')
    check_status = serializers.CharField(required=False, help_text='审核状态')

    # 针对用户进行查询
    owner__user_name = serializers.CharField(help_text='医生姓名', required=False)
    owner__hospital = serializers.CharField(help_text='医生所在医院', required=False)

    class Meta:
        model = questionnaire.QuestionNaire
        fields = [
            'patient_name', 'patient_phone', 'hospital_name',
            'patient_birth', 'career', 'check_status',
            'owner__user_name', 'owner__hospital'
        ]


class CheckStatusSerializer(serializers.Serializer):
    CHECKED_STATUS = [i[0] for i in questionnaire.QuestionNaire.CHECKED_CHOICE]
    CHECKED_CHOICE = questionnaire.QuestionNaire.CHECKED_CHOICE

    check_status = serializers.ChoiceField(required=True, choices=CHECKED_CHOICE, help_text='审核状态')
    reason_for_check = serializers.CharField(required=False, help_text='审核不通过原因')

    def validate(self, value):
        status = value.get('check_status')
        if status not in self.CHECKED_STATUS:
            raise serializers.ValidationError('非法审核状态')

        reason = value.get('reason_for_check')

        if status == '审核不通过' and not reason:
            raise serializers.ValidationError('审核不通过的原因未填写.')

        return value


class QuestionNaireLinkSerializer(ExtendsSerializer, serializers.HyperlinkedModelSerializer):
    check = serializers.HyperlinkedIdentityField(read_only=True, view_name='prj003-info-check', lookup_field='pk',)
    summary = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-summary-detail')
    history = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-history-detail')
    relevant = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-relevant-detail')
    clinical = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-clinical-detail')
    cure = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-cure-detail')
    results = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj003-results-detail')
    info = serializers.HyperlinkedIdentityField(read_only=True, view_name='prj003-question-detail', lookup_field='pk')
    owner_id = serializers.PrimaryKeyRelatedField(label='辅助医生ID', read_only=True)
    owner_name = serializers.StringRelatedField(label='辅助医生姓名', read_only=True, source='owner.user_name')

    class Meta:
        model = questionnaire.QuestionNaire
        fields = '__all__'
        read_only_fields = ['owner', 'serial', 'check_status', 'reason_for_check']
        extra_kwargs = {
            'url': {'view_name': 'prj003-question-detail', 'lookup_field': 'pk'},
        }
        project_name = 'prj003'
        save_mobile = False


class SummarySerializer(SCSerializer):

    class Meta:
        model = summary.Summary
        fields = '__all__'
        project_name = 'prj003'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-summary-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }


class HistorySerializer(SCSerializer):

    class Meta:
        model = history.History
        fields = '__all__'
        project_name = 'prj003'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-history-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }


class RelevantSerializer(SCSerializer):

    class Meta:
        model = relevant.Relevant
        fields = '__all__'
        project_name = 'prj003'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-relevant-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }


class ClinicalSerializer(SCSerializer):

    class Meta:
        model = clinical.Clinical
        fields = '__all__'
        project_name = 'prj003'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-clinical-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }


class CureSerializer(SCSerializer):

    class Meta:
        model = cure.Cure
        project_name = 'prj003'
        fields = '__all__'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-cure-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }


class ResultsSerializer(SCSerializer):

    class Meta:
        model = results.Results
        fields = '__all__'
        project_name = 'prj003'
        read_only_fields = ['owner']
        extra_kwargs = {
            'url': {'view_name': 'prj003-results-detail', 'lookup_field': 'pk'},
            'info': {'view_name': 'prj003-question-detail', }
        }
